package ma.platform.server.upms.rpc;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import ma.platform.server.core.exception.ErrorCodes;
import ma.platform.server.core.util.MsgUtils;
import ma.platform.server.core.util.R;
import ma.platform.server.upms.api.dto.UserDTO;
import ma.platform.server.upms.api.dto.UserInfo;
import ma.platform.server.upms.api.entity.SysUser;
import ma.platform.server.upms.api.rpc.RemoteUserService;
import ma.platform.server.upms.service.SysUserService;
import org.springframework.stereotype.Component;

/**
 * Copyright:
 * Author:
 * Description:
 * Created by 李林山 on 2024/8/18
 */
@Component
public class RemoteUserServiceImpl implements RemoteUserService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public R<UserInfo> info(UserDTO userDTO) {
        SysUser user = sysUserService.getOne(Wrappers.<SysUser>query()
                .lambda()
                .eq(StrUtil.isNotBlank(userDTO.getUsername()), SysUser::getUsername, userDTO.getUsername())
                .eq(StrUtil.isNotBlank(userDTO.getPhone()), SysUser::getPhone, userDTO.getPhone()));
        if (user == null) {
            return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_USERINFO_EMPTY, userDTO.getUsername()));
        }
        return R.ok(sysUserService.findUserInfo(user));
    }
}
